package top.yuyufeng.mbuy.web;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.yuyufeng.mbuy.dto.PageQuery;
import top.yuyufeng.mbuy.dto.ProductDTO;
import top.yuyufeng.mbuy.dto.ResponseJson;
import top.yuyufeng.mbuy.service.IProductService;

/**
 * @author yyf19
 */
@Controller
@RequestMapping(value = "product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @RequestMapping(value = "toPage", method = RequestMethod.GET)
    public String toPage() {
        return "product-page";
    }

    @RequestMapping(value = "page", method = RequestMethod.POST)
    public @ResponseBody
    ResponseJson<PageInfo<ProductDTO>> page(@RequestBody PageQuery pageQuery) {
        PageInfo<ProductDTO> pageInfo = productService.page(pageQuery);
        return ResponseJson.success(pageInfo);
    }

    @RequestMapping(value = "toGet/{id}", method = RequestMethod.GET)
    public String toContent(Model model, @PathVariable("id") Integer id) {
        model.addAttribute("productId", id);
        return "product-content";
    }

    @RequestMapping(value = "get/{id}", method = RequestMethod.GET)
    public @ResponseBody
    ResponseJson<ProductDTO> get(@PathVariable("id") Integer id) {
        ProductDTO productDTO = productService.get(id);
        return ResponseJson.success(productDTO);
    }



}
